﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace Crypt_Tranceiver
{
    class RSA_file
    {
        public string str_Private_Key;
        public string str_Public_Key;

        public void GenKey()
        {
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();

            str_Private_Key = Convert.ToBase64String(RSAalg.ExportCspBlob(true));
            str_Public_Key = Convert.ToBase64String(RSAalg.ExportCspBlob(false));
        }

        //对数据签名
        public string HashAndSign(string str_DataToSign, string str_Private_Key)
        {
            UTF8Encoding ByteConverter = new UTF8Encoding();
            byte[] DataToSign = ByteConverter.GetBytes(str_DataToSign);
            try
            {
                RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();
                RSAalg.ImportCspBlob(Convert.FromBase64String(str_Private_Key));
                byte[] signedData = RSAalg.SignData(DataToSign, new SHA1CryptoServiceProvider());
                string str_SignedData = Convert.ToBase64String(signedData);
                return str_SignedData;
            }
            catch (CryptographicException e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }

        //验证签名
        public bool VerifySignedHash(string str_DataToVerify, string str_SignedData, string str_Public_Key)
        {
            byte[] SignedData = Convert.FromBase64String(str_SignedData);

            UTF8Encoding ByteConverter = new UTF8Encoding();
            byte[] DataToVerify = ByteConverter.GetBytes(str_DataToVerify);
            try
            {
                RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();
                RSAalg.ImportCspBlob(Convert.FromBase64String(str_Public_Key));

                return RSAalg.VerifyData(DataToVerify, new SHA1CryptoServiceProvider(), SignedData);

            }
            catch (CryptographicException e)
            {
                Console.WriteLine(e.Message);

                return false;
            }
        }

        //对数据签名
        public byte[] HashAndSign(byte[] DataToSign, string str_Private_Key)
        {
            UTF8Encoding ByteConverter = new UTF8Encoding();
            try
            {
                RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();//创建RSA加密器
                RSAalg.ImportCspBlob(Convert.FromBase64String(str_Private_Key));//导入私钥
                byte[] signedData = RSAalg.SignData(DataToSign, new SHA1CryptoServiceProvider());//计算签名
                return signedData;
            }
            catch (CryptographicException e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }

        //验证签名
        public bool VerifySignedHash(byte[] DataToVerify, byte[] SignedData, string str_Public_Key)
        {

            UTF8Encoding ByteConverter = new UTF8Encoding();
            try
            {
                RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();//创建RSA加密器
                RSAalg.ImportCspBlob(Convert.FromBase64String(str_Public_Key));//导入公钥
                return RSAalg.VerifyData(DataToVerify, new SHA1CryptoServiceProvider(), SignedData);//返回签证结果
            }
            catch (CryptographicException e)
            {
                Console.WriteLine(e.Message);

                return false;
            }
        }
    }
}
